package com.zxw.article.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zxw.article.service.TagService;
import com.zxw.common.api.ApiResult;
import com.zxw.common.api.PageResult;
import com.zxw.common.constant.ApiConst;
import com.zxw.common.entity.tag.TagEntity;
import com.zxw.common.entity.tag.TagListQuery;
import com.zxw.common.entity.tag.TagQuery;
import com.zxw.common.entity.tag.TagVO;
import com.zxw.common.enums.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 标签信息表接口
 * </p>
 *
 * @author zhanxuewei
 * @since 2022-09-19
 */
@Api(tags = "标签信息表接口", description = "TagController")
@RestController
@RequestMapping(ApiConst.API_PREFIX +"/tag")
public class TagController {

    @Autowired
    private TagService tagService;

    @ApiOperation("添加")
    @PostMapping("/save")
    public ApiResult<ResultCode> save(@RequestBody TagQuery tagQuery) {
        tagService.save(tagQuery);
        return ApiResult.success(ResultCode.SUCCESS);
    }

    @ApiOperation("列表条件分页查询")
    @GetMapping("/pageList")
    public ApiResult<PageResult<TagVO>> pageList(TagListQuery tagListQuery) {
        IPage<TagVO> iPage = this.tagService.pageList(tagListQuery);
        return ApiResult.success(PageResult.restPage(iPage, iPage.getRecords()));
    }

    @ApiOperation("列表条件查询")
    @GetMapping("/tagList")
    public ApiResult<List<TagVO>> queryList() {
        List<TagVO> list = this.tagService.queryList();
        return ApiResult.success(list);
    }

    @ApiOperation("批量添加")
    @PostMapping("/saveBatch")
    public ApiResult<ResultCode> saveBatch(@RequestBody List<TagQuery> list) {
        tagService.saveBatch(list);
        return ApiResult.success(ResultCode.SUCCESS);
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    public ApiResult<ResultCode> update(@RequestBody TagQuery tagQuery) {
        tagService.updateById(tagQuery);
        return ApiResult.success(ResultCode.SUCCESS);
    }

    @ApiOperation("批量删除")
    @PostMapping("/deletes")
    public ApiResult<ResultCode> deletes(@RequestBody List<String> ids) {
        tagService.removeByIds(ids);
        return ApiResult.success(ResultCode.SUCCESS);
    }

    @ApiOperation("详情")
    @GetMapping("/detail/{id}")
    public ApiResult<TagVO> detail(@PathVariable("id") String id) {
        TagEntity tagEntity = tagService.getById(id);
        TagVO tagVO = new TagVO();
        BeanUtils.copyProperties(tagEntity, tagVO);
        return ApiResult.success(tagVO);
    }

}

